# @Time : 2020/8/27 15:23
# @Author : Fioman 
# @Phone : 13149920693
"""
我们一般如何计算一个图像的边缘呢?
1> 计算图像的梯度
图像的梯度表示的是图像强度的方向变化
梯度值 = sqrt(square(Gx)+square(Gy))
2> 梯度方向
梯度方向 角度值 = arctan2(Gy,Gx)*180/pi
"""
import cv2 as cv
import numpy as np

image = cv.imread("pic/16.png")
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
cv.imshow("Original",image)

gX = cv.Sobel(gray,ddepth=cv.CV_64F,dx=1,dy=0)
gY = cv.Sobel(gray,ddepth=cv.CV_64F,dx=0,dy=1)
gX = cv.convertScaleAbs(gX)
gY = cv.convertScaleAbs(gY)
sobelXY = cv.addWeighted(gX,0.5,gY,0.5,0)

cv.imshow("SobelX",gX)
cv.imshow("SobelY",gY)
cv.imshow("SobelXY",sobelXY)
cv.waitKey(0)
cv.destroyAllWindows()


image = cv.imread("pic/14.png")
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
cv.imshow("Original",image)

sobelX  = cv.Sobel(gray,cv.CV_64F,1,0)
sobelY  = cv.Sobel(gray,cv.CV_64F,0,1)


sobelGradient = np.sqrt(np.square(sobelX),np.square(sobelY))
orientation = np.arctan2(sobelY,sobelX) * (180 / np.pi) % 180
idxs = np.where(orientation >= 175,orientation,-1)
idxs = np.where(orientation <= 180,idxs,-1)

mask = np.zeros(gray.shape,dtype=np.uint8)
mask[idxs>-1] = 255
cv.imshow("Mask",mask)
cv.waitKey(0)



